package com.ybl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ybl.common.CustomException;
import com.ybl.dto.DishDto;
import com.ybl.entity.Dish;
import com.ybl.entity.DishFlavor;
import com.ybl.entity.Setmeal;
import com.ybl.entity.SetmealDish;
import com.ybl.mapper.DishMapper;
import com.ybl.service.DishFlavorService;
import com.ybl.service.DishService;
import com.ybl.service.SetmealDishService;
import com.ybl.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 新增菜品，同时保存对应的口味数据
     */
    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        this.save(dishDto);
        Long dishId = dishDto.getId();
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 根据id查询菜品信息和对应的口味信息
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品基本信息，从dish表查询
        Dish dish = this.getById(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);
        //查询当前菜品对应的口味信息，从dish_flavor表查询
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(flavors);
        return dishDto;
    }

    /**
     * 修改菜品信息和对应口味数据
     */
    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        //更新dish表基本信息
        this.updateById(dishDto);
        //清理当前菜品对应口味数据---dish_flavor表的delete操作
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(queryWrapper);
        //添加当前提交过来的口味数据---dish_flavor表的insert操作
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 根据id删除菜品数据和对应口味数据
     */
    @Override
    public void getByIdDeleteWithFlavor(List<Long> ids) {
        //只有不关联套餐的菜品才可以删除
        LambdaQueryWrapper<SetmealDish> TQW = new LambdaQueryWrapper<>();
        for (Long aLong : ids) {
            TQW.eq(SetmealDish::getDishId, aLong);
            int Tcount = setmealDishService.count(TQW);
            if (Tcount > 0) {
                Dish byId = this.getById(aLong);
                //如果不能删除，抛出一个业务异常
                throw new CustomException("菜品" + byId.getName() + "已关联套餐，无法删除--此次删除操作失败！");
            }
        }
        //只有停售的菜品才可以删除
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Dish::getId, ids);
        queryWrapper.eq(Dish::getStatus, 1);
        int count = this.count(queryWrapper);
        if (count > 0) {
            //如果不能删除，抛出一个业务异常
            throw new CustomException("菜品正在售卖中，不能删除");
        }
        //删除dish表的数据
        this.removeByIds(ids);
        //清理当前菜品对应口味数据---dish_flavor表的delete操作
        LambdaQueryWrapper<DishFlavor> Dqw = new LambdaQueryWrapper<>();
        for (Long id : ids) {
            Dqw.eq(DishFlavor::getDishId, id);
            Dqw.or();
        }
        dishFlavorService.remove(Dqw);
    }

    /**
     * 根据id修改状态
     */
    @Override
    public void getByIdUpdaterState(Byte state, List<Long> ids) {
        //批量更改,只有状态一致的菜品才可以向下，否则抛出业务异常
        if (ids.size() > 1) {
            LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.in(Dish::getId, ids);
            byte b;
            if (state == 1) {
                b = 0;
            } else {
                b = 1;
            }
            queryWrapper.eq(Dish::getStatus, b);
            int count = this.count(queryWrapper);
            if (!(count == ids.size())) {
                throw new CustomException("状态不一致，操作失败");
            }
        }
        //菜品关联套餐，不能停售
        LambdaQueryWrapper<SetmealDish> TQW = new LambdaQueryWrapper<>();
        for (Long aLong : ids) {
            TQW.eq(SetmealDish::getDishId, aLong);
            int Tcount = setmealDishService.count(TQW);
            if (Tcount > 0) {
                Dish byId = this.getById(aLong);
                //如果不能删除，抛出一个业务异常
                throw new CustomException("菜品" + byId.getName() + "--关联套餐，不能停售");
            }
        }
        //更改单个，批量经过判断后来此更改
        LambdaQueryWrapper<Dish> DQW = new LambdaQueryWrapper<>();
        for (Long id : ids) {
            DQW.eq(Dish::getId, id);
            DQW.or();
        }
        Dish dish = new Dish();
        dish.setStatus(Integer.valueOf(state));
        this.update(dish, DQW);
    }
}
